package com.skyvin.nreceipts.common.controller;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.skyvin.nreceipts.common.document.AppUser;
import com.skyvin.nreceipts.common.value.MessageValue;
import com.skyvin.nreceipts.common.value.MobileFileUpload;
import com.skyvin.nreceipts.receipts.service.ReceiptUploadService;

@Controller
@RequestMapping(value="mobile")
public class MobileController {
	private static final Logger logger = LoggerFactory
			.getLogger(MobileController.class);

	@Autowired 
	ReceiptUploadService receiptUploadService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public @ResponseBody
	String login(@RequestParam(value = "userid") String userid,
			@RequestParam(value = "password") String password) {

		logger.info("USERID Passed : " + userid);
		logger.info("PASSWORD Passed : " + password);

		String xmlReturnString = "";

		if (userid.equals("karolstuart@yahoo.com") && password.equals("karol")) {
			xmlReturnString = "<response><loginstatus>1</loginstatus><message>Login Successful</message><userid>"
					+ userid + "</userid></response>";
		} else {
			xmlReturnString = "<response><loginstatus>0</loginstatus><message>Invalid userid/password</message><userid>"
					+ userid + "</userid></response>";
		}
		return xmlReturnString;
	}

	@RequestMapping(value = "/loadLoginPage", method = RequestMethod.GET)
	public String loadLoginPage() {

		return "mobilelogin";
	}
	
	@RequestMapping(value = "/loadSignPage", method = RequestMethod.GET)
	public String loadSignupPage(Model model) {
		AppUser appUser = new AppUser();
		model.addAttribute("appUser", appUser);
		return "mobilesignup";
	}
	
	@RequestMapping(value = "/loadUploadPage", method = RequestMethod.GET)
	public String loadUploadPage(Model model) {
		MobileFileUpload mobileFileUpload = new MobileFileUpload();
		model.addAttribute("mobileFileUpload", mobileFileUpload);
		return "mobileupload";
	}
	
	@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
	public @ResponseBody MessageValue handleFormUpload(MobileFileUpload mobileFileUpload
			) throws IOException {
		MessageValue message = new MessageValue();
		logger.info("Mobile File upload passed : " + mobileFileUpload) ; 

			if (!mobileFileUpload.getFile().isEmpty()) {
				CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) mobileFileUpload.getFile();
				// Creating receipt upload document. 
/*				ReceiptUpload receiptUpload = new ReceiptUpload();
				receiptUpload.setUserPID("");
				receiptUpload.setReceiptFile(commonsMultipartFile);*/
				
				receiptUploadService.uploadFile(commonsMultipartFile);
				
				// receiptUploadService.uploadFileandSaveReceipt(receiptUpload);

				message.setMessage("File is uploaded...");

			} else {
				message.setMessage("File is empty.. Could not upload");
			}
		return message;
	}
}
